package com.iwomy.envpro.platform.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iwomy.envpro.platform.entity.AirAbnormalEventEntity;
import com.iwomy.envpro.platform.entity.AirSiteEntity;
import com.iwomy.envpro.platform.mapper.AirAbnormalEventMapper;
import com.iwomy.envpro.platform.service.AirAbnormalEventService;
import com.iwomy.envpro.platform.service.AirSiteService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 异常事件表
 *
 * @author pig
 * @date 2023-08-29 17:58:11
 */
@Service
public class AirAbnormalEventServiceImpl extends ServiceImpl<AirAbnormalEventMapper, AirAbnormalEventEntity> implements AirAbnormalEventService {
	@Resource
	private AirSiteService airSiteService;
	@Override
	public Page getAirAbnormalEventList(Page page, AirAbnormalEventEntity airAbnormalEvent) {

		Page airSiteList = airSiteService.getAirSiteList(page, airAbnormalEvent.getAlarmSiteId());
		List<AirSiteEntity> records = airSiteList.getRecords();
		List<Long> collect  =records.stream().map(AirSiteEntity::getId).collect(Collectors.toList());

		airAbnormalEvent.setAlarmSiteId(null);
		LambdaQueryWrapper<AirAbnormalEventEntity> queryWrapper = Wrappers.lambdaQuery(airAbnormalEvent);
		if(ObjectUtil.isEmpty(collect)) {
			return new Page();
		}
		queryWrapper.in(AirAbnormalEventEntity::getAlarmSiteId, collect);
		return this.page(page,queryWrapper);
	}
}